<?php

$oDb = &Database::get();
$oTpl = &App::getTemplate();
$oReq = &App::getRequest();

if ( $oReq->get('import') )
{
	$aImports = $oReq->getArray('option');

	if ( count($aImports) > 0 )
	{
		foreach($aImports as $aImport)
		{
			$oDb->insert(App::getT('sys_sett'), $aImport);
		}

		if ( isset($_SESSION['build']['settings']) )
		{
			App::gotoUrl('admin.frames.main');
		}

		App::gotoUrl('admin.config.check', array(), App::format('Mod_Admin.import_options_complete'), false);
	}
}

$sFile = App::getParam('path') . 'include/settings/backup/phpfox-settings.php';

if ( file_exists($sFile) )
{
	require_once $sFile;

	if ( isset($_OPTIONS) )
	{
		$aItems = $oDb->getRows("SELECT sett_id, code FROM ". App::getT('sys_sett') ."");
		foreach($aItems as $aItem)
		{
			$iCode = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('sys_sett') ." WHERE code = '". $aItem['code'] ."'");
			if ( $iCode > 1 )
			{
				$oDb->_delete(App::getT('sys_sett'), "sett_id = '". $aItem['sett_id'] ."'");
			}
		}

		$aMissingOptions = array();
		$i = 0;
		foreach($_OPTIONS as $aOption)
		{
			$aRow = $oDb->getRow("SELECT sett_id, block, settype FROM ". App::getT('sys_sett') ." WHERE `code` = '". $aOption['code'] ."'");
			if ( !isset($aRow['sett_id']) )
			{
				$i++;
				$aMissingOptions[] = $aOption;
			}
			else
			{
				$aQuery = array();
				if ($aOption['block'] != $aRow['block'])
				{
					$aQuery['block'] = $aOption['block'];
				}
				if ($aOption['settype'] != $aRow['settype'])
				{
					$aQuery['settype'] = $aOption['settype'];
				}
				if (count($aQuery))
				{
					$oDb->update(App::getT('sys_sett'), $aQuery, "sett_id = '". $aRow['sett_id'] ."'");
				}
			}
		}
	}

	if ( count($aMissingOptions) && $oReq->get('build') )
	{
		$_SESSION['build']['settings'] = true;
	}
}

$oTpl->assign(array
(
	'aMissings' => (isset($aMissingOptions) ? $aMissingOptions : ''),
	'i' => (isset($i) ? $i : ''),
));

?>